import json
import os
import sys
from datetime import datetime
# 将项目根目录添加到 Python 路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
from models import db
from models.user import User
from main import app

def migrate_users():
    with open('data/users.json', 'r') as f:
        users_data = json.load(f)
    
    with app.app_context():
        for username, data in users_data.items():
            user = User(
                username=username,
                password=data['password'],  # 注意：实际应用中应该重新哈希密码
                created_at=datetime.strptime(data['created_at'], '%Y-%m-%d %H:%M:%S.%f')
            )
            db.session.add(user)
        db.session.commit()

if __name__ == '__main__':
    migrate_users()